<?php namespace App\Services\FindModuleService;

/**
 * 发现页后台管理
 *
 * 发现页后台管理service
 *
 * @author    zhouchao <zhouchao@yunys.com.cn>
 * @copyright 2016 QuanZhouYunYiShang Inc.
 */
use App\Services\BaseService;
use App\Services\ProductsService\ProductsBackService;
use App\Services\TypesService\TypesBackService;
use App\Services\UserService\UserTool;
use DB;

class FindModuleBackstageService extends BaseService
{
    private $relate_module = [1, 2, 3, 4, 5, 10];
    protected $types_back_service;
    public function __construct(TypesBackService $types_back_service, UserTool $user_tool, ProductsBackService $products_back_service)
    {
        $this->types_back_service = $types_back_service;
        $this->user_tool = $user_tool;
        $this->products_back_service = $products_back_service;
    }

    //获取模块列表
    public function getList()
    {
        $this->setParameter(array(
            'table' => 'find_modules',
            'where' => array(array('state', '=', 1)),
            'select' => array('find_module_id', 'find_module_name', 'app_id', 'is_show', 'updated_at', 'sort'),
            'sort' => 'desc',
            'order' => 'sort',
        ));
        $findModules = $this->getAll()->toArray();
        foreach ($findModules as $findModule) {
            $findModule->app_cn_name = $this->get_app_cn_app_name_by_app_id($findModule->app_id);
        }
        return $findModules;
    }

    public function save_module($data)
    {
        $this->table = 'find_modules';
        $data['created_at'] = $data['updated_at'] = time();
        $result = $this->save($data);
        return $result;
    }

    /**
     * 删除模块元素
     * @param $find_module_id
     * @param $data
     * @return mixed
     */
    public function delete_module_resources($find_module_id, $data)
    {
        $result = $this->update_module_by_id($find_module_id, $data);
        if (!empty($result)) {
            $this->update_module_resources_by_id($find_module_id, $data);
        }
        return $result;

    }

    public function get_all_relate_modules()
    {
        $relate_modules = array();
        $i = 0;
        foreach ($this->relate_module as $value) {
            $relate_modules[$i]['app_cn_name'] = $this->get_app_cn_app_name_by_app_id($value);
            $relate_modules[$i]['app_id'] = $value;
            $i++;
        }
        return $relate_modules;
    }

    public function update_module_resources_by_id($find_module_id, $data)
    {
        $this->setParameter([
            'table' => 'find_module_resources',
            'where' => [['find_module_id', '=', $find_module_id]],
        ]);
        $result = $this->update($data);
        return $result;
    }

    public function update_module_by_id($find_module_id, $data)
    {
        $this->setParameter([
            'table' => 'find_modules',
            'where' => [['find_module_id', '=', $find_module_id]],
        ]);
        $result = $this->update($data);
        return $result;
    }

    /**
     *
     * @param $find_module_id
     */
    public function get_type_by_module_id($find_module_id)
    {
        //获取已添加
        $this->setParameter([
            'table' => 'find_module_resources',
            'where' => [['find_module_id', '=', $find_module_id], ['state', '=', 1]],
        ]);
        $resource_ids = $this->pluck('resource_id')->toArray();
        $this->types_back_service->setParameter([
            'table' => 'types',
            'where' => [['state', '=', 1], ['is_show', '=', 1]],
            'whereIn' => $resource_ids,
            'select' => ['type_id', 'type_name', 'type_image', 'app_id', 'is_features'],
            'order' => 'sort',
            'sort' => 'desc',
        ]);
        $types = $this->types_back_service->getAllNotInResourceIds('type_id')->toArray();
        foreach ($types as $type) {
            $type->type_image = env('IMG_URL_STD_PREFIX') . $type->type_image;
            $type->app_cn_name = $this->get_app_cn_app_name_by_app_id($type->app_id);
        }
        return $types;
    }

    /**
     * 插入数据
     * @param $type_id
     * @param $problem_ids
     */
    public function module_add_data($find_module_id, $resource_data_ids)
    {
        //获取已添加
        $this->setParameter([
            'table' => 'find_module_resources',
            'where' => [['find_module_id', '=', $find_module_id]],
        ]);
        $resource_ids = $this->pluck('resource_id')->toArray();
        if (!empty($resource_ids)) {
            $intersect_arr = array_intersect($resource_ids, $resource_data_ids);
            $resource_data_ids = array_diff($resource_data_ids, $resource_ids);
        }
        $data = array();
        $datas = array();
        foreach ($resource_data_ids as $data_id) {
            //插入
            $data['find_module_id'] = $find_module_id;
            $data['resource_id'] = $data_id;
            $data['created_at'] = time();
            $data['updated_at'] = time();
            $datas[] = $data;
        }
        $result = DB::table('find_module_resources')->insert($datas);
        //更新
        if (isset($intersect_arr) && !empty($intersect_arr)) {
            foreach ($intersect_arr as $value) {
                $this->setParameter([
                    'table' => 'find_module_resources',
                    'where' => [['find_module_id', '=', $find_module_id], ['resource_id', '=', $value]],
                ]);
                $this->update(array('state' => 1, 'updated_at' => time()));
            }
        }
    }

    public function get_module_data_list($app_id, $find_module_id)
    {
        $this->setParameter([
            'table' => 'find_module_resources',
            'where' => [['find_module_id', '=', $find_module_id], ['state', '=', 1]],
            'select' => ['find_module_resource_id', 'resource_id', 'find_module_id', 'sort'],
            'order' => 'sort',
            'sort' => 'desc',
        ]);
        $resource_ids = $this->pluck('resource_id');
        $resource = $this->getAll()->toArray();
        if ($app_id == 1) {
            return array();
        } elseif ($app_id == 2) {
            //推荐设计师
            foreach ($resource as $key => $value) {
                $user = $this->user_tool->userByUserid($value->resource_id);
                if (empty($user)) {
                    unset($resource[$key]);
                }
                $value->user = $user;
            }
            //$users = $this->user_tool->userListByUserid($resource_ids);
            return $resource;
        } elseif ($app_id == 3) {
            //产品
            foreach ($resource as $key => $value) {
                $product = $this->products_back_service->get_product_by_product_id($value->resource_id);
                if (empty($product)) {
                    unset($resource[$key]);
                }
                $value->product = $product;
            }

            //$products = $this->products_back_service->get_products_by_product_ids($resource_ids);
            return $resource;
        } elseif ($app_id == 4) {
            //主题
            foreach ($resource as $key => $value) {
                $type = $this->types_back_service->get_type_by_type_id($value->resource_id);
                if (empty($type)) {
                    unset($resource[$key]);
                }
                $value->type = $type;
            }
            //$types =  $this->types_back_service->getListByReSourceIds($resource_ids);
            return $resource;
        } else {
            //推荐品牌商
            foreach ($resource as $key => $value) {
                $user = $this->user_tool->userByUserid($value->resource_id);
                if (empty($user)) {
                    unset($resource[$key]);
                }
                $value->user = $user;
            }
            //$users = $this->user_tool->userListByUserid($resource_ids);
            return $resource;
        }
    }

    public function delete_resource($module_id, $resource_id)
    {
        $this->setParameter([
            'table' => 'find_module_resources',
            'where' => [['find_module_id', '=', $module_id], ['resource_id', '=', $resource_id], ['state', '=', 1]],
        ]);
        $result = $this->update(array('updated_at' => time(), 'state' => 2));
        return $result;
    }

    public function update_module_resources_sort($module_id, $resource_id, $data)
    {
        $this->setParameter([
            'table' => 'find_module_resources',
            'where' => [['find_module_id', '=', $module_id], ['resource_id', '=', $resource_id], ['state', '=', 1]],
        ]);
        $result = $this->update($data);
        return $result;
    }
}
